finally working version after fixing hash access issues

Umar M. Sheikh 10 ans auparavant
Parent
Commettre
f7f10656d0
2 fichiers modifiés avec 40 ajouts et 11 suppressions
  1. 5 11
      app/models/agents/public_transport_agent.rb
  2. 35 0
      spec/data_fixtures/public_transport_agent.xml

+ 5 - 11
app/models/agents/public_transport_agent.rb

@@ -46,14 +46,6 @@ module Agents
46 46
             }
47 47
       }
48 48
     MD
49
-    def session
50
-      @session ||= Patron::Session.new
51
-      @session.connect_timeout = 10
52
-      @session.timeout = 60
53
-      @session.headers['Accept-Language'] = 'en-us,en;q=0.5'
54
-      @session.headers['User-Agent'] = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.12 (KHTML, like Gecko) Chrome/9.0.584.0 Safari/534.12"
55
-      @session
56
-    end
57 49
 
58 50
     def check_url
59 51
       stop_query = URI.encode(options["stops"].collect{|a| "&stops=#{a}"}.join)
@@ -76,8 +68,10 @@ module Agents
76 68
             vals = vals.merge Hash.from_xml(pr.to_xml)
77 69
             if not_already_in_memory?(vals)
78 70
               create_event(:payload => vals)
71
+              log "creating event..."
79 72
               update_memory(vals)
80 73
             else
74
+              log "not creating event since already in memory"
81 75
             end
82 76
           end
83 77
         end
@@ -91,14 +85,14 @@ module Agents
91 85
     end
92 86
     def cleanup_old_memory
93 87
       self.memory["existing_routes"] ||= []
94
-      self.memory["existing_routes"].reject!{|h| h["currentTime"] <= (Time.now - 2.hours)}
88
+      self.memory["existing_routes"].reject!{|h| h["currentTime"].to_time <= (Time.now - 2.hours)}
95 89
     end
96 90
     def add_to_memory(vals)
97 91
       self.memory["existing_routes"] ||= []
98
-      self.memory["existing_routes"] << {stopTag: vals["stopTag"], tripTag: vals["prediction"]["tripTag"], epochTime: vals["prediction"]["epochTime"], currentTime: Time.now}
92
+      self.memory["existing_routes"] << {"stopTag" => vals["stopTag"], "tripTag" => vals["prediction"]["tripTag"], "epochTime" => vals["prediction"]["epochTime"], "currentTime" => Time.now}
99 93
     end
100 94
     def not_already_in_memory?(vals)
101
-      m = self.memory["existing_routes"]
95
+      m = self.memory["existing_routes"] || []
102 96
       m.select{|h| h['stopTag'] == vals["stopTag"] &&
103 97
                 h['tripTag'] == vals["prediction"]["tripTag"] &&
104 98
                 h['epochTime'] == vals["prediction"]["epochTime"]

+ 35 - 0
spec/data_fixtures/public_transport_agent.xml

@@ -0,0 +1,35 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<body copyright="All data copyright San Francisco Muni 2014.">
3
+<predictions agencyTitle="San Francisco Muni" routeTitle="N-Judah" routeTag="N" stopTitle="Judah St &amp; La Playa St" stopTag="5221">
4
+  <direction title="Outbound to Ocean Beach">
5
+  <prediction epochTime="1389707083293" seconds="1668" minutes="27" isDeparture="false" affectedByLayover="true" dirTag="N__OB3" vehicle="1443" block="9705" tripTag="5840326"/>
6
+  <prediction epochTime="1389708835605" seconds="3420" minutes="57" isDeparture="false" affectedByLayover="true" dirTag="N__OB3" vehicle="1518" block="9708" tripTag="5840327"/>
7
+  <prediction epochTime="1389709795605" seconds="4380" minutes="73" isDeparture="false" affectedByLayover="true" dirTag="N__OB3" vehicle="1404" block="9710" tripTag="5840328"/>
8
+  </direction>
9
+  <direction title="Outbound to Ocean Beach via Downtown">
10
+  <prediction epochTime="1389706393991" seconds="978" minutes="16" isDeparture="false" dirTag="N__OB4KJU" vehicle="1543" vehiclesInConsist="2" block="9703" tripTag="5840324"/>
11
+  <prediction epochTime="1389706512784" seconds="1097" minutes="18" isDeparture="false" dirTag="N__OB4KJU" vehicle="1476" vehiclesInConsist="2" block="9704" tripTag="5840083"/>
12
+  <prediction epochTime="1389707746994" seconds="2331" minutes="38" isDeparture="false" dirTag="N__OB4KJU" vehicle="1507" block="9706" tripTag="5840084"/>
13
+  <prediction epochTime="1389708458668" seconds="3043" minutes="50" isDeparture="false" affectedByLayover="true" dirTag="N__OB4KJU" vehicle="1489" block="9707" tripTag="5840085"/>
14
+  <prediction epochTime="1389709358668" seconds="3943" minutes="65" isDeparture="false" affectedByLayover="true" dirTag="N__OB4KJU" vehicle="1463" block="9709" tripTag="5840086"/>
15
+  </direction>
16
+<message text="No Elevator at
17
+Van Ness Station"/>
18
+</predictions>
19
+<predictions agencyTitle="San Francisco Muni" routeTitle="N-Judah" routeTag="N" stopTitle="Judah St &amp; 46th Ave" stopTag="5215">
20
+  <direction title="Outbound to Ocean Beach">
21
+  <prediction epochTime="1389706981164" seconds="1566" minutes="26" isDeparture="false" affectedByLayover="true" dirTag="N__OB3" vehicle="1443" block="9705" tripTag="5840326"/>
22
+  <prediction epochTime="1389708733476" seconds="3318" minutes="55" isDeparture="false" affectedByLayover="true" dirTag="N__OB3" vehicle="1518" block="9708" tripTag="5840327"/>
23
+  <prediction epochTime="1389709693476" seconds="4278" minutes="71" isDeparture="false" affectedByLayover="true" dirTag="N__OB3" vehicle="1404" block="9710" tripTag="5840328"/>
24
+  </direction>
25
+  <direction title="Outbound to Ocean Beach via Downtown">
26
+  <prediction epochTime="1389706282012" seconds="866" minutes="14" isDeparture="false" dirTag="N__OB4KJU" vehicle="1543" vehiclesInConsist="2" block="9703" tripTag="5840324"/>
27
+  <prediction epochTime="1389706400805" seconds="985" minutes="16" isDeparture="false" dirTag="N__OB4KJU" vehicle="1476" vehiclesInConsist="2" block="9704" tripTag="5840083"/>
28
+  <prediction epochTime="1389707635015" seconds="2219" minutes="36" isDeparture="false" dirTag="N__OB4KJU" vehicle="1507" block="9706" tripTag="5840084"/>
29
+  <prediction epochTime="1389708346689" seconds="2931" minutes="48" isDeparture="false" affectedByLayover="true" dirTag="N__OB4KJU" vehicle="1489" block="9707" tripTag="5840085"/>
30
+  <prediction epochTime="1389709246689" seconds="3831" minutes="63" isDeparture="false" affectedByLayover="true" dirTag="N__OB4KJU" vehicle="1463" block="9709" tripTag="5840086"/>
31
+  </direction>
32
+<message text="No Elevator at
33
+Van Ness Station"/>
34
+</predictions>
35
+</body>